<template>
    <div class="app-container">
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['plan:workSpace:add']"
                >新增</el-button>
            </el-col>
            <!--  -->
            <right-toolbar :search="false" @queryTable="reflash"></right-toolbar>
        </el-row>
        <h1>Today</h1>
        <el-table v-loading="loading" :data="contentList">
            <el-table-column label="事件标题" align="center" prop="title" show-overflow-tooltip/>
            <el-table-column label="事件内容" align="center" prop="content" show-overflow-tooltip/>
            <el-table-column label="学习状态" align="center" key="studyStatus">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.studyStatus"
                        active-value="1"
                        inactive-value="0"
                        @change="handleStatusChange(scope.row)"
                    ></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="describes" show-overflow-tooltip/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-view"
                        @click="handleDetail(scope.row)"
                        v-hasPermi="['plan:content:edit']"
                    >详情</el-button>                    
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['plan:content:edit']"
                    >note</el-button>
                </template>
            </el-table-column>
        </el-table>
        
        <h1>Review</h1>
        <el-table v-loading="loading" :data="reviewList">
            <el-table-column label="事件标题" align="center" prop="title" show-overflow-tooltip/>
            <el-table-column label="事件内容" align="center" prop="content" show-overflow-tooltip/>
            <el-table-column label="复习状态" align="center" key="reviewStatus">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.reviewStatus"
                        active-value="1"
                        inactive-value="0"
                        @change="handleReviewStatusChange(scope.row)"
                    ></el-switch>
                </template>
            </el-table-column>            
            <el-table-column label="备注" align="center" prop="describes" show-overflow-tooltip/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-view"
                        @click="handleDetail(scope.row)"
                        v-hasPermi="['plan:content:edit']"
                    >详情</el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['plan:content:edit']"
                    >note</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 详情弹窗 -->
        <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">    
                <el-form-item label="科目" prop="subjectId">
                    <el-select v-model="form.subjectId" placeholder="科目" :disabled="addbtn">
                        <el-option
                            v-for="dict in subjectEnmu"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        ></el-option>
                    </el-select>
                </el-form-item>             
                <el-form-item label="事件标题" prop="title">
                    <el-input v-model="form.title" placeholder="请输入事件标题" :disabled="addbtn"/>
                </el-form-item>
                <el-form-item label="事件内容" prop="content">
                    <editor v-model="form.content" :min-height="192" />                    
                </el-form-item>
                                                              
                <el-form-item label="备注" prop="describes">
                    <el-input v-model="form.describes" type="textarea" placeholder="请输入内容" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm" v-show ="submitBtn">确 定</el-button>
                <el-button @click="cancel">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listSubject} from "@/api/plan/subject";
import { todyTodoList,todyReviewList} from "@/api/plan/workSpace";
import { updateContent,getContent,addContent} from "@/api/plan/content";
import { updateReview} from "@/api/plan/review";

export default {
    name: "Content",
    dicts: ['is_quene', 'is_study_wether'],
    data() {
      return {
        submitBtn:true,
        subjectEnmu:[],
        // showSearch:false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 新增按钮禁用
        addbtn: false,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 学习内容表格数据
        contentList: [],
        // 复习内容表格数据
        reviewList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
          subjectId:null
        },
         // 表单参数
      form: {},
      // 表单校验
      rules: {
        subjectId: [
          { required: true, message: "学科表的外键不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "事件标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "事件内容不能为空", trigger: "blur" }
        ],
        studyStatus: [
          { required: true, message: "学习的状态不能为空", trigger: "change" }
        ],
        markStatus: [
          { required: true, message: "今日内容展示状态不能为空", trigger: "change" }
        ],
        isQueue: [
          { required: true, message: "任务开启排队不能为空", trigger: "change" }
        ],
        createDate: [
          { required: true, message: "事件创建日期不能为空", trigger: "blur" }
        ],
      }
      };
    },
    created() {
    //   this.getTodyTodoList();
    //   this.getTodyReviewList();
        this.reflash();
        this.getSubjectList();
    },
    methods: {
        //刷新
        reflash(){
            this.getTodyTodoList();
            this.getTodyReviewList();
        },
        /** 查询科目列表  新增今日学习内容时使用 */
        getSubjectList() {
            let allSubject = {};
            listSubject(allSubject).then(response => {
                let List = response.rows;
                this.subjectEnmu = [];
                for (let index = 0; index < List.length; index++) {
                    const element = List[index];
                    let obj ={
                        value:element.id,
                        label:element.pName,
                    }
                    this.subjectEnmu.push(obj);
                }
            });
        },
        /** 查询学习内容列表 */
        getTodyTodoList() {
            this.loading = true;
            todyTodoList().then(response => {
                this.contentList = response.rows;
                this.loading = false;
            });
        },
        /** 查询复习内容列表 */
        getTodyReviewList() {
            this.loading = true;
            todyReviewList().then(response => {
                this.reviewList = response.rows;
                this.loading = false;
            });
        },
        // 用户状态修改  updateContent
        handleStatusChange(row) {
            let text = row.studyStatus === "1" ? "标注任务完成" : "标注任务未完成";
            this.$modal.confirm('确认要' + text +'吗？').then(function() {
                return updateContent(row);
            }).then(() => {
                this.$modal.msgSuccess(text);// + "成功"
            }).catch(function() {
                row.studyStatus = row.studyStatus === "0" ? "1" : "0";
            });
        },
        // 用户状态修改  updateReview
        handleReviewStatusChange(row) {
            let text = row.reviewStatus === "1" ? "标注复习任务完成" : "标注复习任务未完成";
            this.$modal.confirm('确认要' + text +'吗？').then(function() {
                let review = {
                    id:row.rid,
                    reviewStatus:row.reviewStatus
                }
                return updateReview(review);
            }).then(() => {
                this.$modal.msgSuccess(text);//+ "成功"
            }).catch(function() {
                row.reviewStatus = row.reviewStatus === "0" ? "1" : "0";
            });
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                subjectId: null,
                title: null,
                content: null,
                studyStatus: null,
                markStatus: null,
                isQueue: null,
                createDate: null,
                describes: null
            };
            this.resetForm("form");
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.addbtn = false;
        },        
        /** 详情按钮操作 */
        handleDetail(row) {
            const id = row.id || this.ids
            getContent(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.addbtn = true;
                this.submitBtn = false;
                this.title = "内容详情";
            });
        },   
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.addbtn = false;
            this.title = "添加学习内容";
        },             
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getContent(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.addbtn = true;
                this.submitBtn = true;
                this.title = "编辑内容";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {                
                if (valid) {
                    if (this.form.id != null) {
                        updateContent(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.reflash();
                        });
                    } else {
                        addContent(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.reflash();
                        });
                    }
                }
            });
        },
    }
};
</script>